package com.ruoyi.query;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.query.entity.TableQueryColumn;
import com.ruoyi.query.service.TableQueryColumnService;

import java.util.Collections;
import java.util.List;

/**
 * **********************
 * * Author: XiaShiLong *
 * * Date  : 2021-04-17 *
 * * Time  : 08:15      *
 * * to    : lz&xm      *
 * **********************
 * 查询列缓存
 **/
public class QueryColumnUtils {

    private static final String REDIS_KEY = "sys_query_column:";

    /**
     * 设置缓存
     *
     * @param key              参数键
     * @param tableQueryColumn tableQueryColumn列表
     */
    public static void setCache(Long key, List<TableQueryColumn> tableQueryColumn) {
        SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(key), tableQueryColumn);
    }

    /**
     * 获取缓存
     *
     * @param key 参数键
     * @return hov hov数据列表
     */
    public static List<TableQueryColumn> getCache(Long key) {
        Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
        if (StringUtils.isNotNull(cacheObj)) {
            return StringUtils.cast(cacheObj);
        } else {
            List<TableQueryColumn> list = SpringUtils.getBean(TableQueryColumnService.class).get(key);
            if (StringUtils.isNotEmpty(list)) {
                setCache(key, list);
                return list;
            }
        }
        return Collections.emptyList();
    }

    /**
     * 删除key
     *
     * @param configKey key关键字
     */
    public static void clearCache(Long configKey) {
        SpringUtils.getBean(RedisCache.class).deleteObject(getCacheKey(configKey));
    }


    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(Long configKey) {
        return REDIS_KEY + configKey;
    }
}
